<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    class="flex"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'"
                >

                <template v-if="route.name == 'xClearrecord/link'">
                    <FormItem :label="t('xClearrecord.TZID')" type="string" v-model="baTable.form.items!.TZID" prop="TZID" :input-attr="{ placeholder: t('Please input field', { field: t('xClearrecord.TZID') }) }" />
                    <FormItem 
                    :label="t('xClearrecord.YWY')" 
                    v-model="baTable.form.items!.YWY" 
                    prop="YWY"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'id',
                        field: 'nickname',
                        'remote-url':authAdmin+'index',
                        placeholder: t('Click Select'),
                        disabled:true
                    }"
                    />
                    <FormItem :label="t('xClearrecord.YWTIME')" type="datetime" v-model="baTable.form.items!.YWTIME" prop="YWTIME" :input-attr="{ placeholder: t('Please select field', { field: t('xClearrecord.YWTIME') }),disabled:true }" />
                    <el-divider />
                </template>
                <FormItem 
                    :label="t('xClearrecord.TCNO')" 
                    v-model="baTable.form.items!.TCNO" 
                    prop="TCNO" 
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'TCNO',
                        field: 'TCNO',
                        'remote-url': '/admin/XPot/index?search[]=%7B%22field%22:%22STATUS%22,%22val%22:%22SYZ%22,%22operator%22:%22%3D%22%7D',
                    }" 
                />
                <FormItem :label="t('xClearrecord.CLNO')" type="string" v-model="baTable.form.items!.CLNO" prop="CLNO" :input-attr="{ placeholder: '保存后自动生成',disabled:true }" />
                <FormItem :label="t('xClearrecord.YARD')" 
                v-model="baTable.form.items!.YARD" 
                prop="YARD" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'CORP',
                    field: 'CORP',
                    'remote-url': '/admin/corplib/index?type=2',
                }" />
                <FormItem :label="t('xClearrecord.GJENDDATE')" type="datetime" v-model="baTable.form.items!.GJENDDATE" prop="GJENDDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xClearrecord.GJENDDATE') }) }" />
                <FormItem :label="t('xClearrecord.CLRPRODUCT')" type="string" v-model="baTable.form.items!.CLRPRODUCT" prop="CLRPRODUCT" :input-attr="{ placeholder: t('Please input field', { field: t('xClearrecord.CLRPRODUCT') }) }" />
                <!-- <FormItem :label="t('xClearrecord.CLRCOST')" type="string" v-model="baTable.form.items!.CLRCOST" prop="CLRCOST" :input-attr="{ placeholder: t('Please input field', { field: t('xClearrecord.CLRCOST') }) }" /> -->
                <FormItem :label="t('xClearrecord.SJENDDATE')" type="datetime" v-model="baTable.form.items!.SJENDDATE" prop="SJENDDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xClearrecord.SJENDDATE') }) }" />
                <FormItem :label="t('xClearrecord.MONEY')" type="number" prop="MONEY" v-model.number="baTable.form.items!.MONEY" :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('xClearrecord.MONEY') }) }" />
                <!-- 清洁检验日期(字段缺失) -->
                <FormItem style="width:100%" :label="t('xClearrecord.FINISHFILE')" type="file" v-model="baTable.form.items!.FINISHFILE" prop="FINISHFILE" />
                <FormItem style="width:100%" :label="t('xClearrecord.OFFERDEMO')" type="textarea" v-model="baTable.form.items!.OFFERDEMO" prop="OFFERDEMO" :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('xClearrecord.OFFERDEMO') }) }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" />
                <FormItem :label="t('xClearrecord.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: t('当前用户'),disabled:true }" />
                <FormItem :label="t('xClearrecord.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: t('当前日期'),disabled:true}" />
                <template v-if="route.name !== 'xClearrecord/link'">
                    <FormItem 
                    :label="t('xClearrecord.YWY')" 
                    v-model="baTable.form.items!.YWY" 
                    prop="YWY"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'id',
                        field: 'nickname',
                        'remote-url':authAdmin+'index',
                        placeholder: t('Click Select'),
                    }"
                    />
                    <FormItem :label="t('xClearrecord.TZID')" type="string" v-model="baTable.form.items!.TZID" prop="TZID" :input-attr="{ placeholder: t('Please input field', { field: t('xClearrecord.TZID') }),disabled:true }" />
                    <FormItem :label="t('xClearrecord.YWTIME')" type="datetime" v-model="baTable.form.items!.YWTIME" prop="YWTIME" :input-attr="{ placeholder: t('Please select field', { field: t('xClearrecord.YWTIME') }),disabled:true }" />
                </template>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { authAdmin} from '/@/api/controllerUrls'
import { useRoute } from 'vue-router'


const route = useRoute()

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    YWY: [buildValidatorData({ name: 'required', title: t('xClearrecord.YWY') })],
    GJENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xClearrecord.GJENDDATE') })})],
    SJENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xClearrecord.SJENDDATE') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xClearrecord.OPTIME') })})],
    YWTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xClearrecord.YWTIME') })})],
})

</script>

<style scoped lang="scss"></style>
